package com.nuonuo.middleware.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.support.ServletConfigPropertySource;
import org.springframework.web.context.support.ServletContextPropertySource;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author duan
 * @version 1.0
 * @date 2023/8/10 11:22
 */
@RestController
public class HelloController {

    @Autowired
    private Environment environment;
    @Resource
    private ApplicationContext context;

    private static final List<String> values = new ArrayList<>(10);

    private static final AtomicInteger count = new AtomicInteger(1);



    public String add() {
        return "OK";
    }

    @GetMapping(value = "/")
    public String index() {
        return "9091";
    }

    @GetMapping(value = "/test/sayHello")
    public String sayHello() throws InterruptedException {
        Thread.sleep(10000000);
        return "test, hello world";
    }

    @PostMapping(value = "/open/invoice/callback.do")
    public void callback() {

    }

    @GetMapping(value = "/test")
    public String getByKey(String key) {
        return environment.getProperty(key);
    }

    @GetMapping(value = "getAllConfig")
    public String getAllConfig() throws IOException {
        ConfigurableEnvironment environment = (ConfigurableEnvironment) context.getEnvironment();
        String targetPath = "F:\\tmp\\config.txt";
        FileWriter writer = new FileWriter(targetPath);
        BufferedWriter bw = new BufferedWriter(writer);
        for (PropertySource<?> propertySource : environment.getPropertySources()) {
            String sourceName = propertySource.getName();
            if (propertySource instanceof MapPropertySource) {
                MapPropertySource source = (MapPropertySource) propertySource;
                for (String propertyName : source.getPropertyNames()) {
                    System.out.println(propertyName + ", " + source.getProperty(propertyName));
                    bw.write(sourceName + "," + propertyName + "," +  source.getProperty(propertyName) + "\n");
                }
            } else if (propertySource instanceof CompositePropertySource) {
                CompositePropertySource source = (CompositePropertySource) propertySource;
                for (String propertyName : source.getPropertyNames()) {
                    System.out.println(propertyName + ", " + source.getProperty(propertyName));
                    bw.write(sourceName + "," + propertyName + "," +  source.getProperty(propertyName) + "\n");
                }
            } else if (propertySource instanceof ServletConfigPropertySource) {
                ServletConfigPropertySource source = (ServletConfigPropertySource) propertySource;
                for (String propertyName : source.getPropertyNames()) {
                    System.out.println(propertyName + ", " + source.getProperty(propertyName));
                    bw.write(sourceName + "," + propertyName + "," +  source.getProperty(propertyName) + "\n");
                }
            } else if (propertySource instanceof ServletContextPropertySource) {
                ServletContextPropertySource source = (ServletContextPropertySource) propertySource;
                for (String propertyName : source.getPropertyNames()) {
                    System.out.println(propertyName + ", " + source.getProperty(propertyName));
                    bw.write(sourceName + "," + propertyName + "," +  source.getProperty(propertyName) + "\n");
                }
            } else {
                bw.write(sourceName + "\n");

            }
        }
        return "OK";
    }
}
